package com.macro.mall.portal.controller.commission;

import com.macro.mall.commission.domain.UserAccount;
import com.macro.mall.commission.entity.UserAccountEntity;
import com.macro.mall.commission.service.UserAccountService;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户佣金账户控制器
 *
 * @author xuyanjun
 * @date 2019-12-23
 */
@Api(tags = "UserAccountController", description = "用户佣金账户")
@Slf4j(topic = "UserAccountController")
@RestController
@RequestMapping("/userAccount")
public class UserAccountController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 根据id获取用户佣金账户
     *
     * @param id id
     * @return 用户佣金账户
     */
    @ApiOperation("根据id获取用户佣金账户")
    @GetMapping("{id}")
    public CommonResult<UserAccount> getById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException("要获取的用户佣金账户Id不能为空。");
        }

        UserAccountEntity userAccountEntity = userAccountService.getById(id);
        if (userAccountEntity == null) {
            throw new BusinessException(String.format("要获取的用户佣金账户id:%s不存在。", id));
        }

        final UserAccount userAccount = userAccountService.entityToDomain(userAccountEntity);

        return CommonResult.success(userAccount);
    }

}

